package com.zhuiyun.project.api.imagesorting.service.impl;

import com.zhuiyun.project.api.file.entity.FileManage;
import com.zhuiyun.project.api.file.service.FileManageService;
import com.zhuiyun.project.api.imagesorting.entity.ImageSorting;
import com.zhuiyun.project.api.imagesorting.mapper.ImageSortingMapper;
import com.zhuiyun.project.api.imagesorting.service.ImageSortingService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName imageSortingServiceImpl
 * @Description 图片排序service实现类
 * @Author zsc
 * @Date 2024/11/11 19:07
 */
@Service
public class ImageSortingServiceImpl implements ImageSortingService {
    @Autowired
    private ImageSortingMapper imageSortingMapper;
    String projectName = "other";
    @Autowired
    private FileManageService fileManageService;
    /**
    * @Author zsc
    * @Description 新增图片排序
    * @Date 2024/11/11 19:29
    * @Return int
    * @param imageSorting
    */
    @Override
    @Transactional
    public int saveImageSorting(ImageSorting imageSorting , MultipartFile file) throws CommonException {
        if ( file != null ){
            FileManage fileManage = fileManageService.saveFileToSystem(file,projectName);
            if(fileManage.getId()>0){
                imageSorting.setImgId(fileManage.getId());
            }
        }
        return imageSortingMapper.saveImageSorting(imageSorting);
    }

    /**
    * @Author zsc
    * @Description 删除图片排序
    * @Date 2024/11/11 19:38
    * @Return int
    * @param
    */
    @Override
    @Transactional
    public int deleteImageSorting(int id , int imgId ) throws CommonException {
        if (imgId > 0 ){
            CommonResult result = fileManageService.deleteImageById(imgId);
        }
        int i = imageSortingMapper.deleteImageSorting(id);
        return i ;
    }

    /**
    * @Author zsc
    * @Description 修改图片排序
    * @Date 2024/11/11 19:48
    * @Return int
    * @param imageSorting
    */
    @Override
    @Transactional
    public int updateImageSorting(ImageSorting imageSorting, MultipartFile file) throws CommonException {
        if (file != null){
            fileManageService.deleteImageById(imageSorting.getImgId());
            FileManage fileManage = fileManageService.saveFileToSystem(file,projectName);
            if(fileManage.getId()>0){
                imageSorting.setImgId(fileManage.getId());
            }
        }
        int i = imageSortingMapper.updateImageSorting(imageSorting);
        return i ;
    }

    /**
    * @Author zsc
    * @Description 查询图片排序
    * @Date 2024/11/11 19:53
    * @Return java.util.List<com.zhuiyun.project.api.imagesorting.entity.ImageSorting>

    */
    @Override
    public List<ImageSorting> getImageSortingList() {
        List<ImageSorting> lists= imageSortingMapper.getImageSortingList();
        return lists;
    }
}
